package nature.nano.control.script.run.message;

import java.util.ArrayList;
import java.util.List;

import nature.nano.component.EmptyProps;
import nature.nano.component.PrimaryStorer;
import nature.nano.component.Strings;
import nature.nano.iface.Accepter;
import nature.nano.iface.Props;

public class FinishedMessager extends PrimaryStorer {

	private List<Props> errors = new ArrayList<Props>();
	private Props timeInfo;
	
	@Override
	public void anBegin(String type, Props props) {
		if (type.equals(Strings.error)) {
			errors.add(props);
		}
		if (type.equals(Strings.elapsedTime)) {
			timeInfo = props;
		}
	}

	@Override
	public void start() {
		Accepter accepter = slot(Strings.output);
		
		accepter.onBegin("DONE", EmptyProps.instance);
		
		if (errors.size() != 0) {
			for (Props prop : errors) {
				accepter.onBegin("Script-Error", prop);
				accepter.onEnd("Script-Error");							
			}
		}
				
		if (timeInfo != null) {
			accepter.onBegin("Elapsed-Time", timeInfo);
			accepter.onEnd("Elapsed-Time");
		} else {
			accepter.onContent("! script run failed because errors");
		}
		
		accepter.onEnd("DONE");
	}
	
}
